#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
 
int main(void)
{
	int ret = -1;
	// 1、使用socket()函数获取一个socket文件描述符
	int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (-1 == sockfd)
	{
		printf("socket open err.");
		return -1;
	}
 
    // 2、绑定本地的相关信息，如果不绑定，则系统会随机分配一个端口号
    struct sockaddr_in local_addr = {0};
    local_addr.sin_family = AF_INET;                                //使用IPv4地址
    //local_addr.sin_addr.s_addr = inet_addr("192.168.0.107");        //本机IP地址
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    local_addr.sin_port = htons(8266);                             //端口
    bind(sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr));//将套接字和IP、端口绑定
 
    // 3、等待接收对方发送的数据
    struct sockaddr_in recv_addr;
    socklen_t addrlen = sizeof(recv_addr);
    char recvbuf[1024] = {0};
    int img_head[2];
    double img_head1[2];
    //ret = recvfrom(sockfd, recvbuf, 1024, 0,(struct sockaddr*)&recv_addr,&addrlen);  //1024表示本次接收的最大字节数
    //ret = recvfrom(sockfd, (char*)img_head, 1024, 0,(struct sockaddr*)&recv_addr,&addrlen);  //1024表示本次接收的最大字节数
    ret = recvfrom(sockfd, (char*)img_head1, 1024, 0,(struct sockaddr*)&recv_addr,&addrlen);  //1024表示本次接收的最大字节数
    //printf("[recv from %s:%d]%s \n",inet_ntoa(*(struct in_addr*)&recv_addr.sin_addr.s_addr),ntohs(recv_addr.sin_port),recvbuf);
    printf("[recv from %s:%d]%f:%f \n",inet_ntoa(*(struct in_addr*)&recv_addr.sin_addr.s_addr),ntohs(recv_addr.sin_port),img_head1[0],img_head1[1]); //打印double浮点数
 
    // 4. 关闭套接字
    close(sockfd);
}

